package cuong.ptit.androidvn.viewcontent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.kefirsf.bb.BBProcessorFactory;
import org.kefirsf.bb.TextProcessor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;
import cuong.ptit.andoridvn.R;
import cuong.ptit.androidvn.helper.JSONParser;

/*
 * @author LeCuong 
 * @version 1.0
 */

public class ViewPost extends Activity {
	private String message;
	private ProgressDialog pDialog;

	

	JSONParser jsonParser = new JSONParser();
	private final static String url_comment = "http://android.vn/api.php?action=getPosts&limit=1&thread_id=41831";
	private final static String TAG_COMMENT = "message";

	private TextView tv;
	private WebView webView;
	private Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewpost);

		/*
		 * try { SmiliesConverter.readFile(this); } catch (IOException e) { //
		 * TODO Auto-generated catch block e.printStackTrace(); } catch
		 * (JSONException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } summary = "";
		 */
		new LoadData().execute();

	}

	final class MyJavaScriptInterface {
		public void ProcessJavaScript(final String scriptname, final String args) {
			mHandler.post(new Runnable() {
				public void run() { // Do your activities
				}
			});
		}
	}

	class LoadData extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			pDialog = new ProgressDialog(ViewPost.this);
			pDialog.setMessage("Loading...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			List<NameValuePair> paramsNumMess = new ArrayList<NameValuePair>();
			String json_user = jsonParser.makeHttpRequest(url_comment, "GET",
					paramsNumMess);
			Log.d("json	", json_user);

			try {
				JSONObject posts_all = new JSONObject(json_user);
				JSONObject postall = posts_all.getJSONObject("posts");

				Iterator<?> keys_all = postall.keys();

				while (keys_all.hasNext()) {
					String key = (String) keys_all.next();

					JSONObject c = postall.getJSONObject(key);
					message = c.getString(TAG_COMMENT);
					Log.d("mess", message);

				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;

		}

		@Override
		protected void onPostExecute(String result) {
			pDialog.dismiss();
			runOnUiThread(new Runnable() {
				@SuppressLint("SetJavaScriptEnabled")
				public void run() {

					try {
						SmiliesConverter.readFile(ViewPost.this);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					webView = (WebView) findViewById(R.id.webView1);
					webView.getSettings().setJavaScriptEnabled(true);
					webView.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
					webView.loadUrl("about:blank");
					TextProcessor processor = BBProcessorFactory.getInstance().create();
					String htmlCode = processor.process(message);
					
					
					htmlCode=SmiliesConverter.ConverteSmiles(htmlCode); 
					webView.loadDataWithBaseURL("fake://not/needed", htmlCode, "text/html","utf-8", "");
					
					

				}
			});
			super.onPostExecute(result);
		}

	}

}
